function [dE1,YREAL,YSIMUL,lambda]=reg2(it,ig,r)

% marcelo 08:33 PM 27/08/1998

% Carrega conjunto de dados e torna global
dataj1;
[IN,RIN]   = normvc(IN,1.2);
[OUT,ROUT] = normvc(OUT,1.2);

lamda = [ 0 0 0 ];
for c=1:3;
	X  = IN (:,it);
	XG = IN (:,ig);
	Y  = OUT(c,it);
	YG = OUT(c,ig);
	C  = X;
	% Spread
	% Calculo da matriz de projeto H_ij=h_j(x_i)
	H=rbfdsgn(X,C,r(c),'g');
	% Regularizacao - valor de lambda
	[lambda(c),erro]=glbridge(H,Y',r(c),'BIC');
	% Vetor otimo de pesos, pseudo inversa
	w=inv(H'*H+lambda(c)*eye(size(H,1)))*H'*Y';
	% Teste da rede
	HG=rbfdsgn(XG,C,r(c),'g');
	YE(:,c)=HG*w;
	% Erro
	Err(:,c)=sum((YE(:,c)-YG').^2)';
end
YE=YE';
Y=[];
j=1:length(ig);

for c=1:3;
	YER(c,:)=valoreal(YE(c,:),ROUT(c,:));
	Y  (c,:)=valoreal(OUT(c,ig),ROUT(c,:));
end;

dE2 = abs(YER-Y);
YREAL  = Y;
YSIMUL = YER;

if (length(ig)>1)
	dE1=sum(dE2')/length(ig);
else
	dE1=dE2';
end;


